/**
 * Helper Class for tree manipulation
 * @class Footbar.TreeUtil
 * @static
 */
if (typeof Footbar == "undefined") {
    Footbar = {};
}


Footbar.TreeUtil={

    bOriginalSelectEventsSuppressed:false,
    selection:null,
/**
 * Expand the tree nodes
 * @param {Object} treeid
 */
    expand:function(treeid){
        var tree=document.getElementById(treeid);
        var selection = tree.view.selection;
        //Prevent firing of onSelect event
        const bOriginalSelectEventsSuppressed = selection.selectEventsSuppressed;
        selection.selectEventsSuppressed = true;
        tree.view.selection.clearSelection();
        tree.view.selection.select(-1);
        var childElements=tree.getElementsByTagName("treeitem");
        //<treeitem  ="true" open="true">
        for(var i=0;i<childElements.length;i++){
            if(childElements[i].hasAttribute("container") && childElements[i].getAttribute("container")){
                childElements[i].setAttribute("open","true");
            }
        }
        selection.selectEventsSuppressed = bOriginalSelectEventsSuppressed ;
    },
/**
 * Collapse tree nodes
 * @param {Object} treeid
 */
    collapse:function(treeid){
        var tree=document.getElementById(treeid);
        var selection = tree.view.selection;
        //Prevent firing of onSelect event
        const bOriginalSelectEventsSuppressed = selection.selectEventsSuppressed;
        selection.selectEventsSuppressed = true;
        tree.view.selection.clearSelection();
        tree.view.selection.select(-1);

        var childElements=tree.getElementsByTagName("treeitem");
        //<treeitem  ="true" open="true">
        for(var i=0;i<childElements.length;i++){
            if(childElements[i].hasAttribute("container") && childElements[i].getAttribute("container")){
                childElements[i].setAttribute("open","false");
            }
        }
        selection.selectEventsSuppressed = bOriginalSelectEventsSuppressed ;
    },
/**
 * Supprese firing of default events on the tree
 * @param {Object} treeViewSelection
 */
    suppresEvents:function(treeViewSelection){

        if(!(treeViewSelection instanceof Object)){
             var tree=document.getElementById(treeViewSelection)
             treeViewSelection=tree.selection;
        }

        this.selection = treeViewSelection
        this.bOriginalSelectEventsSuppressed = this.selection.selectEventsSuppressed;
        //Prevent firing of onSelect event
        this.selection.selectEventsSuppressed = true;
        this.selection.clearSelection();
        this.selection.select(-1);
    },
/**
 * Restore last suppresed Events
 */
    restoreSuppresedEvents:function(){
        this.selection.selectEventsSuppressed = this.bOriginalSelectEventsSuppressed;

    },
/**
 * Helper function to insert new tree node
 * @param {Object} data
 */
    cellNode:function(data){
        var	treeitem_sub = document.createElement("treeitem");
        var	treerow_sub = document.createElement("treerow");
        treerow_sub.setAttribute("id",  "setname-treerowsub-2434");
        var label_title_sub= document.createElement("treecell");

        for(x in data){
            label_title_sub.setAttribute(x, data[x]);
        }

        treerow_sub.appendChild(label_title_sub);
        treeitem_sub.appendChild(treerow_sub);
        return treeitem_sub;
    },
/**
 * Mark Selected Row as Read
 * @param {Object} treeId
 */
    markRowAsRead:function(treeId){
        //http://developer.mozilla.org/en/docs/XUL:tree#Examples
        var tree=document.getElementById( treeId );
        if(tree==null){
            alert("TreeUtil.markRowAsRead is unable to find treeID:"+treeId)
        }
        if(!tree.currentIndex && !(tree.currentIndex > -1)){
            return;
        }

        //mark the current row as read
        var selection = tree.contentView.getItemAtIndex(tree.currentIndex);

        //ChildNodes of the Tree
        if(!selection.hasAttribute("container")){
            var cells=selection.getElementsByTagName('treecell');
            for(var k=0;k<cells.length;k++){
                cells[k].setAttribute("properties","read");
            }
        }
    },
/**
 * UnMark Selected Row as Read
 * @param {Object} treeId
 */
    markRowAsUnRead:function(treeId){
         //http://developer.mozilla.org/en/docs/XUL:tree#Examples
        var tree=document.getElementById( treeId );
        if(tree==null){
            alert("TreeUtil.markRowAsUnRead is unable to find treeID:"+treeId)
        }
        if(!tree.currentIndex && !(tree.currentIndex > -1)){
            return;
        }

        //mark the current row as read
        var selection = tree.contentView.getItemAtIndex(tree.currentIndex);

        //ChildNodes of the Tree
        if(!selection.hasAttribute("container")){
            var cells=selection.getElementsByTagName('treecell');
            for(var k=0;k<cells.length;k++){
                cells[k].setAttribute("properties","unread");
            }
        }
    },
/**
 * Return selected tree row ether at level 1 or level 2 : its up to the coder to check for node type
 * @param {Object} treeId
 */
    getSelectedRow:function(treeId){
        var tree=document.getElementById( treeId );
        if(tree==null){
            alert("TreeUtil.markRowAsRead is unable to find treeID:"+treeId)
        }

        if(!tree.currentIndex || !(tree.currentIndex > -1)){
            //return null;
        }
        //dump("\n tree.currentIndex="+tree.currentIndex +" == "+(!tree.currentIndex || !(tree.currentIndex > -1)))

        var selection ;
        try{
            selection = tree.contentView.getItemAtIndex(tree.currentIndex);
        }catch(e){
            //alert("Error - getSelectedRow():"+e);
        }//Suppressed waring

        return selection;
    },
/**
 * Find ower of this node
 * @param {Object} node
 */
    findParentNode:function(node){
        var target=node;
        while(target){
            if(target.hasAttribute('container')){
                break;
            }
            target=target.parentNode;
        }
        return target.getElementsByTagName('treecell')[0];
    }

}
			